﻿///////////////////////////////////////////////////////////////////////////////
//
// This file is part of the BassDrop project
//   https://code.google.com/p/bass-drop/
//
//
// Creator:	Gergely Krajcsovszki (krajcsovszkig@hotmail.com)
// Date:	2013-08-04 17:50
//
//
// Copyright © 2013-2014. Gergely Krajcsovszki
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BassDrop.Core.Common.Plugins;
using BassDrop.Core.Common;
using BassDrop.Plugins.Dropbox.Localization;
using System.Globalization;
using System.Threading;
using BassDrop.Core.Common.Services;
using System.Threading.Tasks;
using System.Reflection;

namespace BassDrop.Plugins.Dropbox
{
	/// <summary>
	/// Description class of the Dropbox plugin
	/// </summary>
	public class BassDropPlugin : BassDropPluginBase
	{
		/// <summary>
		/// Has the module been initialized
		/// </summary>
		private static bool s_Inited = false;

		/// <summary>
		/// Constructs a new information object for the Dropbox plugin
		/// </summary>
		/// <param name="appController">The controller object for the application running the plugin</param>
		/// <param name="pluginPrivateDir">The path of the directory where the plugin can store its stuff</param>
		public BassDropPlugin( Controller appController, string pluginPrivateDir )
			: base( appController, pluginPrivateDir )
		{
			if ( !s_Inited )
			{
				Init( AppController, pluginPrivateDir );
			}

			Name = "Dropbox";
			SetDescriptions();
			PluginType = PluginType.CloudProvider;
			SupportedProtocols = new List<string>()
			{ 
				"Dropbox" 
			};
			Revision = 59;
			Version = "0.6.2000:" + Revision;
			Icon = Assembly.GetExecutingAssembly().GetManifestResourceStream( "BassDrop.Plugins.Dropbox.dropboxlogo.png" );

			PluginStrings.StaticPropertyChanged += PluginStrings_StaticPropertyChanged;
		}

		private void SetDescriptions()
		{
			ShortDescription = PluginStrings.DescriptionPluginShort;
			LongDescription = PluginStrings.DescriptionPluginLong;
		}

		/// <summary>
		/// Initializes the plugin. Must be called before using the plugin
		/// </summary>
		/// <param name="appController">The controller object for the application running the plugin</param>
		/// <param name="pluginPrivateDir">The path of the directory where the plugin can store its stuff</param>
		private static void Init( Controller appController, string pluginPrivateDir )
		{
			// Load languages
			PluginLanguage.ScrapeLanguagesFrom( pluginPrivateDir + "/Languages", ServiceCenter.FileHandler );
		}

		/// <summary>
		/// Sets the language used by the plugin to the specified one
		/// </summary>
		/// <param name="language">the 2 letter code of the language, eg. en</param>
		public override void SetLanguage( string language )
		{
			PluginStrings.LoadStringsFor( PluginLanguage.ForLanguage( language ), ServiceCenter.FileHandler );

			CultureInfo culture = new CultureInfo( PluginStrings.SelectedLanguage.LanguageCode );

			Thread.CurrentThread.CurrentCulture = culture;
			Thread.CurrentThread.CurrentUICulture = culture;
		}

		/// <summary>
		/// Creates a new instance of the plugin with the specified name and loads its settings.
		/// </summary>
		/// <param name="name">the name of the new instance or null to create a temporary one</param>
		/// <param name="settings">the settings object, from where the settings of the new instance can be loaded if they exist</param>
		/// <returns></returns>
		protected override async Task<PluginObjectBase> DoCreateInstance( string name, ISettings settings )
		{
			Connection newConnection = new Connection( this, name );
			await newConnection.Load( settings );

			return newConnection;
		}

		private void PluginStrings_StaticPropertyChanged( object sender, System.ComponentModel.PropertyChangedEventArgs e )
		{
			SetDescriptions();
		}
	}
}
